/**
 * Copyright (c) 2011-2015 1lifes.com
 * HSJ java-handlersocket
 * Id: FindPacket.java
 */
package com.elifes.hsj.packet;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.log4j.Logger;

import com.elifes.hsj.model.CompareOperator;
import com.elifes.hsj.model.Filter;
import com.elifes.hsj.model.INFilter;

/**
 * 描述：
 * @author yangqiang
 * @createtime 2011-11-12下午5:10:53
 *
 */
public class FindPacket extends AbstractPacket{
	private static final Logger logger = Logger.getLogger(FindPacket.class);
	private CompareOperator operator;
	private List<String> filterValues;
	private int limit;
	private int offset;
	private INFilter in;
	private List<Filter> filters;
	private Class modelClass;
	private List<String> properties;
	
	public FindPacket(String indexId, CompareOperator operator,
			List<String> filterValues, int limit, int offset, INFilter in,
			List<Filter> filters, Class modelClass, List<String> properties){
		type = PacketEnum.FIND;
		this.indexId = indexId;
		this.operator = operator;
		this.filterValues = filterValues;
		this.limit = limit;
		this.offset = offset;
		this.in = in;
		this.filters = filters;
		this.modelClass = modelClass;
		this.properties = properties;
	}

	@Override
	protected void encodeHeader(StringBuilder reqMsg) {}

	@Override
	protected void encodeBody(StringBuilder reqMsg) {

		reqMsg.append(autoAppendTab(indexId))
			  .append(autoAppendTab(operator.getValue()))
			  .append(autoAppendTab(String.valueOf(filterValues.size())));
		
		for(String value : filterValues){
				reqMsg.append(autoAppendTab(value));
			
		}
		
		reqMsg.append(autoAppendTab(String.valueOf(limit)))
		      .append(autoAppendTab(String.valueOf(offset)));
	
		if(in != null){
			reqMsg.append(autoAppendTab("@"))
			      .append(autoAppendTab(String.valueOf(in.getCol())))
			      .append(autoAppendTab(String.valueOf(in.getInValues().size())));
			
			for(String value : in.getInValues()){
				reqMsg.append(autoAppendTab(value));
			
			}   
		}
		
		if(filters != null){
			for(Filter filter : filters){
				reqMsg.append(autoAppendTab(filter.getType().getValue()))
				      .append(autoAppendTab(String.valueOf(filter.getColumn())))
				      .append(autoAppendTab(filter.getOperator().getValue()))
				      .append(autoAppendTab(filter.getValue()));
			
			}
		}
	}

	@Override
	public Object decode() {
		Class clazz = null;
		Object model = null;
		List models = new ArrayList();
		try {
			String[] strs = ((String)getResult()).split(TAB_TOKEN);
			
			clazz = Class.forName(modelClass.getName());
			int j = 0;
			for(int i = 2; i < strs.length; i++){
				if((i-2) % Integer.valueOf(strs[1]) == 0){
					model = clazz.newInstance();
					j = 0;
					models.add(model);
				}
				
				BeanUtils.setProperty(model, properties.get(j++), strs[i]);
			}
		} catch (ClassNotFoundException e) {
			logger.error(e);
		} catch (InstantiationException e) {
			logger.error(e);
		} catch (IllegalAccessException e) {
			logger.error(e);
		} catch (InvocationTargetException e) {
			logger.error(e);
		}
		
		setResult(models);
		return models;
	}

}
